package com.henu.cee_service.controller;

import com.henu.cee_service.entity.News;
import com.henu.cee_service.service.NewsService;
import com.henu.cee_service.utils.DataResult;
import com.henu.cee_service.vo.req.NewsAddReqVO;
import com.henu.cee_service.vo.req.NewsPageReqVO;
import com.henu.cee_service.vo.req.NewsUpdateReqVO;
import com.henu.cee_service.vo.resq.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
//import javax.xml.crypto.Data;

/**
 * description: NewsController
 * date: 2020/9/19 17:12
 * author: JSM
 * version: 1.0
 */

@RestController
@RequestMapping("/v1")
@Api(tags = "新闻模块相关接口")

public class NewsController {

    @Autowired
    private NewsService newsService;

    @PostMapping("/news")
    @ApiOperation(value = "分页查询所有的新闻")
    public DataResult<PageVO<News>> pageInfo(@RequestBody NewsPageReqVO vo) {
        DataResult result = DataResult.success();
        result.setData(newsService.pageInfo(vo));
        return result;
    }

    @PostMapping("/new")
    @ApiOperation(value = "添加新闻")
    public DataResult addNew(@RequestBody @Valid NewsAddReqVO vo) {
        newsService.addNew(vo);
        DataResult result = DataResult.success();
        return result;
    }

    @GetMapping("/new/{id}")
    @ApiOperation(value = "查询新闻详情")
    public DataResult<News> queryNew(@PathVariable("id") Integer id) {
        DataResult<News> result = DataResult.success();
        result.setData(newsService.queryById(id));
        return result;
    }

    @PutMapping("/new")
    @ApiOperation(value = "更新新闻")
    public DataResult updateNew(@RequestBody @Valid NewsUpdateReqVO vo) {
        newsService.updateNew(vo);
        return DataResult.success();
    }

    @DeleteMapping("/new/{id}")
    @ApiOperation(value = "查询新闻详情")
    public DataResult deleteNew(@PathVariable("id") Integer id) {
        newsService.deleteNew(id);
        return DataResult.success();
    }

}
